<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  <script>
    console.time('x')
    // const binary = (n) => {
    //   if(n === 1) {
    //     return [0, 1]
    //   } else {
    //     const prev = binary(n - 1)
    //     const result = []
    //     const max = Math.pow(2, n) - 1
    //     for (let i = 0, li = prev.length; i < li; i++) {
    //       result[i] = `0${prev[i]}`
    //       result[max - i] = `1${prev[i]}`
    //     }
    //     return result
    //   }
    // }
    const binary = (n) => {
      if (n === 0) {
        return [0];
      }

      if (n === 1) {
        return [0, 1];
      }
      let formerGrayCode = binary(n-1);
      return [
        ...formerGrayCode,
        ...formerGrayCode.reverse().map(v => v + Math.pow(2, n-1)),
      ]
    }
    console.log(binary(3));
    
    console.timeEnd('x');
  </script>
</body>
</html>